<template>
  <div class="form-layout" style="height: 400px">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="cpItemList" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="cpPropRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <template v-if="cpShowAudit">
            <div id="box-apply" class="box-title">审核信息</div>
            <customFormItem label="审核时间" name="gmtAudited" :parent="_self" :form="form" disabled full-width/>
            <customFormItem label="审核人" name="auditor" :parent="_self" :form="form" disabled full-width/>
            <customFormItem label="审核备注" name="auditedRemark" :parent="_self" :form="form" disabled full-width/>
          </template>


          <div id="box-apply" class="box-title">申请信息</div>
          <customFormItem label="医院" name="hospitalName" :parent="_self" :form="form" ref="hospitalName"
                          :type="constCommon.formItemType.selectStatic.name" :config="{options: utilVuex.getHospitalList()}"
                          :disabled="!propParent.cpFormNew"
                          help="保存后不可修改"
                          @change="hospitalIdChange"
          />
          <customFormItem label="供应商" name="supplierName" :parent="_self" :form="form" disabled v-if="!utils.isSp()"/>

          <customFormItem label="申请单号" name="code" :parent="_self" :form="form"
                          :type="constCommon.formItemType.staticHtml.name"  :config="{defaultValue: '自动获取'}"/>
          <customFormItem label="状态" name="status" :parent="_self" :form="form"
                          :type="constCommon.formItemType.selectDict.name"
                          :config="{dictType: constCommon.dictType.baseSupplierApplicantStatus}"
                         disabled
                          v-if="!propParent.cpFormNew"
          />
          <customFormItem label="申请说明" name="remark" :parent="_self" :form="form"
                          :type="constCommon.formItemType.inputText.name" full-width/>
          <customFormItem
            label="申请附件"
            name="applyFIle"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
            style="padding-left: 10px;"
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id, fileRefTable: 'material_applicant', fileSavePath: 'material_applicant'}"
                :hide="propParent.cpFormNew"
                hideMsg="请先保存后再上传"
                :max="20"
                :disabled="propReadonly"
                multiple
                theme="file"
              />
            </template>
          </customFormItem>

          <div id="box-dtl" class="box-title">商品明细</div>
          <data-view
            propViewKey="material_applicant_item"
            :propIsTdesign="true"
            :propParentPage="_self"
            ref="childDataView"
            :propAutoFetchData="false"
            :propTableHide="!form.id"
            propTableHideMsg="保存完主单据再操作"
            :propOnlyQueryAll="propReadonly"
            :propInitData="form"
            :propBeforeClick="propBeforeClickDtl"
            :propNoEditRow="true"
          />

        </template>
      </customForm>
    </div>

    <!--选择商品多个-->
    <tableDrawer
      ref="tableDrawerMaterialSpec"
      loadForce
      refName="tableDrawerMaterialSpec"
      viewKey="material_spec_forMaterialApplicantAdd"
      :propUserParam="cpPropUserParam"
      title="请选择商品"
      subtitle="<span class='span-msg'>未供货该医院</span><span class='span-msg'>该供应商</span>"
      :widthPer="80"
      :heightPx="-1"
      :widthPx="-1"
    />
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from '@/components/custom/form/customUpload.vue'

export default {
  help: '供方资格申请-详情',
  data() {
    return {
      form: {},
      hpList: []
    }
  },
  methods: {
    // 加载明细数据
    loadDtl() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('applicantId', this.form.id)
      } else {
        queryWrapper.ne(1, 1)
      }
      this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },

    // 医院选择
    hospitalIdChange(value, item) {
      this.$set(this.form, 'hospitalName', item.name)
      this.$set(this.form, 'hospitalId', item.id)
    },

    // 列表添加按钮
    propBeforeClickDtl(button, formData , rowData, that) {
      //列表添加按钮 其他入库
      if (button.key == 'vb-add') {
        this.$refs.tableDrawerMaterialSpec.show(this.matrSelect, true)
        return false
      }
      return true;
    },
    // 批量保存明细
    matrSelect(rows, isLoadForm) {
      this.utilVue.loadShow(true)
      rows.forEach(row => {
        row.applicantId = this.form.id
        row.materialId = row.materialId
        row.materialSpecId = row.id
        row.id = null
      })
      this.$ajax.put('/base/material_applicant_items/batch_save', rows).then((resp) => {
        this.utilVue.loadHide()
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
          this.loadDtl()
        }
      })
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData) => {
        this.form = global.utils.jsonClone(formData)
        this.$set(this.form, 'id', this.form.id)
        if (this.propParent.cpFormNew) {
          if (!this.utils.isSp()) {
            this.utilMessage.warning('当前非供应商，不可操作')
            this.propParent.setViewFormVisible(false)
            return
          }
          this.$set(this.form, 'supplierId',this.utils.getSpInfo().id)
          this.$refs.hospitalName.focus()
        }
        this.loadDtl()
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      },
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
  },
  components: { customAnchor, customUpload },
  computed: {
    cpPropRules() {
      let rule = this.propRules || {}
      return rule
    },
    cpShowAudit() {
      return [2,3].includes(this.form.status)
    },
    cpItemList() {
      if (this.cpShowAudit) {
        return [
          { name: 'box-audit', title: '审核信息' },
          { name: 'box-apply', title: '申请信息' },
          { name: 'box-dtl', title: '商品明细' },
        ]
      }
      return [
        { name: 'box-apply', title: '申请信息' },
        { name: 'box-dtl', title: '商品明细' },
      ]
    },
    cpPropUserParam() {
      return {
        supplierId: this.form.supplierId,
        hospitalId: this.form.hospitalId,
      }
    }
  },
}
</script>
